package com.itlong.cloud.controller;

import com.itlong.cloud.abstracts.BaseController;
import com.itlong.cloud.annotation.LoggerInfo;
import com.itlong.cloud.constants.LogDescConstant;
import com.itlong.cloud.equip.property.IPropertyEquipServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.user.operate.IOperatePersonClient;
import com.itlong.cloud.user.property.IPropertyPersonServiceClient;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * <desc>
 * 运营系统聚合Controller。
 * </desc>
 *
 * @createDate 2017/08/29
 */
@RestController
public class PropertyDemoController extends BaseController {

    private static Logger LOG = LoggerFactory.getLogger(PropertyDemoController.class);

    @Autowired
    IPropertyPersonServiceClient iPropertyPersonServiceClient;
    @Autowired
    IOperatePersonClient iOperatePersonClient;

    @Autowired
    IPropertyEquipServiceClient iPropertyEquipServiceClient;


    /**
     * <desc>
     * DEMO示例。
     * <desc/>
     *
     * @param param 名称
     * @return hello
     * @author Wenchao.L
     * @createDate 2017/08/29
     */
    @RequestMapping(path = "/hi/{param}", method = RequestMethod.GET)
    public Object hi(@PathVariable(name = "param") String param) {
        try {
            //iPropertyPersonServiceClient.userSayHello(param);
            iPropertyEquipServiceClient.equipSayHello(param);
        } catch (Exception e) {
            throw new DataAccessException("【运营_系统_角色delete】删除资源失败", e);
        }
        //iOperatePersonClient.userSayHello2(param);
        //iOperatePersonClient.userSayHello2(param);
        return "helloWorld!";
    }


    /**
     * <desc>
     * DEMO示例。
     * <desc/>
     *
     * @param param 名称
     * @return hello
     * @author Wenchao.L
     * @createDate 2017/08/29
     */
    @LoggerInfo(operateDesc = LogDescConstant.USER_UPDATE_INFO_LOG_DESC)
    @RequestMapping(path = "/update", method = RequestMethod.GET)
    public Object update(String param) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        param = request.getAttribute("param") == null ? null : request.getAttribute("param").toString();
        iPropertyEquipServiceClient.updateEquipInfo(param);
        return "修改成功!";
    }


/*    public String returnResult1(ResultVO resultVO) {
        String result = null;
        try {
            result = JsonUtil.toJSON(resultVO);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }*/


 /*   @RequestMapping(path = "/synMain", method = RequestMethod.GET)
    public String synMain() {
*//*        ExecutorService exec = Executors.newCachedThreadPool();
        final Semaphore semp = new Semaphore(thread_num);

        for (int index = 0; index < client_num; index++) {

            final int NO = index;

            Runnable run = new Runnable() {
                public void run() {
                    try {

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            };
            exec.execute(run);
        }
        exec.shutdown();*//*
        for (int i = 0; i < 100; i++) {
            new Thread1().start();
            new Thread2().start();
        }
        return "并发测试！";
    }*/


   /* public static void main(String[] args) throws ClientException, InterruptedException, IOException {


        int thread_num = 1000;
        int client_num = 15;

        ExecutorService exec = Executors.newCachedThreadPool();
        final Semaphore semp = new Semaphore(thread_num);

        for (int index = 0; index < client_num; index++) {

            Integer finalIndex = index;
            Runnable run = new Runnable() {
                public void run() {
                    try {
                        new PropertyDemoController().testIot();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            };
            exec.execute(run);
        }
        exec.shutdown();
}*/

     /*   CreateProductRequest request = new CreateProductRequest();
        request.setCatId(10000L);
        request.setDesc("Create Product test");
        request.setName("TestProduct");
        CreateProductResponse response = null;
        try {
            response = client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        if(response != null){
            System.out.println("Response requestId:"+response.getRequestId()+" isSuccess:"+response.getSuccess() +" Error:"+response.getErrorMessage());
        }

    }

    }

    public void stringBuilderTest() {
        long start = System.currentTimeMillis();
        System.out.println("String.fomart cost time:" + (System.currentTimeMillis() - start));
    }
    */
/*
    //服务器发送消息到设备
    public void testIot() throws ClientException, IOException {
        String accessKey = "LTAIieKviG1jBpsn";
        String accessSecret = "D3epXRB6dtWKpRzUDC5w1ZD7OGU3LX";
        DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "Iot", "iot.cn-shanghai.aliyuncs.com");
        IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", accessKey, accessSecret);
        DefaultAcsClient client = new DefaultAcsClient(profile); //初始化SDK客户端

        //deviceName:2IhhGqmScTfsolWQdcyl
        //deviceSecret:3MP95BMX5Uj3699fEgSFGGD8BJ3h5cWF

        PubRequest request = new PubRequest();
        request.setProductKey("DfimdRzkMpB");
        Map<String,String> map = new HashMap<>();
        map.put("message",JsonUtil.toJSON(RandomUtil.getRandomStringByLength(8)+"旺龙云测试阿里云IOT！！！"));
        String s = JsonUtil.toJSON(map);
        request.setMessageContent(Base64.encodeBase64String(s.getBytes()));
        request.setTopicFullName("/DfimdRzkMpB/bulb/get");
        request.setQos(0); //目前支持QoS0和QoS1
        PubResponse response = client.getAcsResponse(request);
        System.out.println(response.getSuccess());
        System.out.println(response.getErrorMessage());
    }*/


}
